<?php
class ModelCatalogKeeplace extends Model {
	public function addKeeplace($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "keeplace` SET keeplace_name = '" . $this->db->escape($data['keeplace_name']) . "', keeplace_address = '" . $this->db->escape($data['keeplace_address']) . "', memo = '" . $this->db->escape($data['memo']) . "'");
	}
	
	public function editKeeplace($keeplace_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "keeplace` SET keeplace_name = '" . $this->db->escape($data['keeplace_name']) . "', keeplace_address = '" . $this->db->escape($data['keeplace_address']) . "', memo = '" . $this->db->escape($data['memo']) . "' WHERE keeplace_id = '" . (int)$keeplace_id . "'");
	}

			
	public function deleteKeeplace($keeplace_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "keeplace` WHERE keeplace_id = '" . (int)$keeplace_id . "'");
	}
	
	public function getKeeplace($keeplace_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "keeplace` WHERE keeplace_id = '" . (int)$keeplace_id . "'");
	
		return $query->row;
	}
	
	public function getKeeplaceByName($name) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "keeplace` WHERE keeplace_name = '" . $this->db->escape($name) . "'");
	
		return $query->row;
	}

		
	public function getKeeplaces($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "keeplace`";
			
		$sort_data = array(
			'keeplace_name',
			'keeplace_address',
			'memo'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY keeplace_name";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}

	public function getTotalKeeplaces() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "keeplace`");
		
		return $query->row['total'];
	}

	
	
	public function getProductByKeeplace($keeplace_id) {
		$query = $this->db->query("SELECT product_id FROM `" . DB_PREFIX . "product` WHERE keeplace_id = '" . (int)$keeplace_id . "'");
		return $query->row;
	}
}
?>
